package com.japstudy.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.japstudy.entity.Question;
import com.japstudy.entity.Word;
import com.japstudy.service.IQuestionService;
import com.japstudy.service.IWordService;

@Controller
@RequestMapping("/Question")
public class QuestionController {

	@Autowired
	private IQuestionService questionServiceImpl;
	@Autowired
	private IWordService wordServiceImpl;
	
	@RequestMapping("historyQuestionUi")
	public String historyQuestionUi() {
		return "user/history_question";
	}
	
	@RequestMapping("lookThisWrongQuestion")
	public String lookThisWrongQuestion(HttpServletRequest request, HttpServletResponse response) throws IOException {
		List<Question> thisTestQuestions = (List<Question>) request.getSession().getAttribute("thisTestQuestion");
		Question question = thisTestQuestions.get(0);
		String yourAnswer = question.getYourAnswer();
		Word answer = new Word();
		if (yourAnswer != null && !yourAnswer.equals("")) {
			if (-1 != yourAnswer.indexOf(";")) {
				String[] yourAnswers = yourAnswer.split(";");
				for (String string : yourAnswers) {
					String[] answerc = string.split("-");
					if(answerc[0].equals("wordChinese")) {
						answer.setWordChinese(answerc[1]);
					} else if(answerc[0].equals("wordPing")) {
						answer.setWordPing(answerc[1]);
					} else if(answerc[0].equals("wordPian")) {
						answer.setWordPian(answerc[1]);
					} else if(answerc[0].equals("wordWord")) {
						answer.setWordWord(answerc[1]);
					}
				} 
			} else {
				String[] answerc = yourAnswer.split("-");
				if(answerc[0].equals("wordChinese")) {
					answer.setWordChinese(answerc[1]);
				} else if(answerc[0].equals("wordPing")) {
					answer.setWordPing(answerc[1]);
				} else if(answerc[0].equals("wordPian")) {
					answer.setWordPian(answerc[1]);
				} else if(answerc[0].equals("wordWord")) {
					answer.setWordWord(answerc[1]);
				}
			}
		}
		request.getSession().setAttribute("yourAnswer", answer);
		request.getSession().setAttribute("nowPageQuestion", thisTestQuestions.get(0));
		request.getSession().setAttribute("nowQuestionIndex", 1);
		request.getSession().setAttribute("maxQuestionIndex", thisTestQuestions.size());
		response.sendRedirect(request.getContextPath() + "/Question/historyQuestionUi");
		return null;
	}
	
	@RequestMapping("/prevQuestion")
	public String prevQuestion(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Integer nowQuestionIndex = (Integer) request.getSession().getAttribute("nowQuestionIndex");
		List<Question> thisTestQuestions = (List<Question>) request.getSession().getAttribute("thisTestQuestion");
		Question question = thisTestQuestions.get(nowQuestionIndex-1-1);
		String yourAnswer = question.getYourAnswer();
		Word answer = new Word();
		if (yourAnswer != null && !yourAnswer.equals("")) {
			if (-1 != yourAnswer.indexOf(";")) {
				String[] yourAnswers = yourAnswer.split(";");
				for (String string : yourAnswers) {
					String[] answerc = string.split("-");
					if(answerc[0].equals("wordChinese")) {
						answer.setWordChinese(answerc[1]);
					} else if(answerc[0].equals("wordPing")) {
						answer.setWordPing(answerc[1]);
					} else if(answerc[0].equals("wordPian")) {
						answer.setWordPian(answerc[1]);
					} else if(answerc[0].equals("wordWord")) {
						answer.setWordWord(answerc[1]);
					}
				} 
			} else {
				String[] answerc = yourAnswer.split("-");
				if(answerc[0].equals("wordChinese")) {
					answer.setWordChinese(answerc[1]);
				} else if(answerc[0].equals("wordPing")) {
					answer.setWordPing(answerc[1]);
				} else if(answerc[0].equals("wordPian")) {
					answer.setWordPian(answerc[1]);
				} else if(answerc[0].equals("wordWord")) {
					answer.setWordWord(answerc[1]);
				}
			}
		}
		request.getSession().setAttribute("nowQuestionIndex", nowQuestionIndex-1);
		request.getSession().setAttribute("nowPageQuestion", question);
		request.getSession().setAttribute("yourAnswer", answer);
		response.sendRedirect(request.getContextPath() + "/Question/historyQuestionUi");
		return null;
	}
	
	@RequestMapping("/nextQuestion")
	public String nextQuestion(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Integer nowQuestionIndex = (Integer) request.getSession().getAttribute("nowQuestionIndex");
		List<Question> thisTestQuestions = (List<Question>) request.getSession().getAttribute("thisTestQuestion");
		Question question = thisTestQuestions.get(nowQuestionIndex);
		String yourAnswer = question.getYourAnswer();
		Word answer = new Word();
		if (yourAnswer != null && !yourAnswer.equals("")) {
			if (-1 != yourAnswer.indexOf(";")) {
				String[] yourAnswers = yourAnswer.split(";");
				for (String string : yourAnswers) {
					String[] answerc = string.split("-");
					if(answerc[0].equals("wordChinese")) {
						answer.setWordChinese(answerc[1]);
					} else if(answerc[0].equals("wordPing")) {
						answer.setWordPing(answerc[1]);
					} else if(answerc[0].equals("wordPian")) {
						answer.setWordPian(answerc[1]);
					} else if(answerc[0].equals("wordWord")) {
						answer.setWordWord(answerc[1]);
					}
				} 
			} else {
				String[] answerc = yourAnswer.split("-");
				if(answerc[0].equals("wordChinese")) {
					answer.setWordChinese(answerc[1]);
				} else if(answerc[0].equals("wordPing")) {
					answer.setWordPing(answerc[1]);
				} else if(answerc[0].equals("wordPian")) {
					answer.setWordPian(answerc[1]);
				} else if(answerc[0].equals("wordWord")) {
					answer.setWordWord(answerc[1]);
				}
			}
		}
		request.getSession().setAttribute("nowQuestionIndex", nowQuestionIndex+1);
		request.getSession().setAttribute("nowPageQuestion", question);
		request.getSession().setAttribute("yourAnswer", answer);
		response.sendRedirect(request.getContextPath() + "/Question/historyQuestionUi");
		return null;
	}
	
	@RequestMapping("selectAllQuestionByTestPaperId")
	public String historyQuestionUi(String testPaperId, HttpServletRequest request, HttpServletResponse response) throws IOException {
		List<Question> thisTestQuestions = questionServiceImpl.selectByTestPaperId(testPaperId);
		for (Question question : thisTestQuestions) {
			String wordId = question.getWord().getWordId();
			Word word = wordServiceImpl.selectById(wordId);
			question.setWord(word);
		}
		Question question = thisTestQuestions.get(0);
		String yourAnswer = question.getYourAnswer();
		Word answer = new Word();
		if (yourAnswer != null && !yourAnswer.equals("")) {
			if (-1 != yourAnswer.indexOf(";")) {
				String[] yourAnswers = yourAnswer.split(";");
				for (String string : yourAnswers) {
					String[] answerc = string.split("-");
					if(answerc[0].equals("wordChinese")) {
						answer.setWordChinese(answerc[1]);
					} else if(answerc[0].equals("wordPing")) {
						answer.setWordPing(answerc[1]);
					} else if(answerc[0].equals("wordPian")) {
						answer.setWordPian(answerc[1]);
					} else if(answerc[0].equals("wordWord")) {
						answer.setWordWord(answerc[1]);
					}
				} 
			} else {
				String[] answerc = yourAnswer.split("-");
				if(answerc[0].equals("wordChinese")) {
					answer.setWordChinese(answerc[1]);
				} else if(answerc[0].equals("wordPing")) {
					answer.setWordPing(answerc[1]);
				} else if(answerc[0].equals("wordPian")) {
					answer.setWordPian(answerc[1]);
				} else if(answerc[0].equals("wordWord")) {
					answer.setWordWord(answerc[1]);
				}
			}
		}
		request.getSession().setAttribute("yourAnswer", answer);
		request.getSession().setAttribute("thisTestQuestion", thisTestQuestions);
		request.getSession().setAttribute("nowPageQuestion", thisTestQuestions.get(0));
		request.getSession().setAttribute("nowQuestionIndex", 1);
		request.getSession().setAttribute("maxQuestionIndex", thisTestQuestions.size());
		response.sendRedirect(request.getContextPath() + "/Question/historyQuestionUi");
		return null;
	}
}
